package org.lhh.server.controller;

import org.lhh.server.annotation.LogCatch;
import org.lhh.server.annotation.UserAuth;
import org.lhh.server.common.api.JSONResult;
import org.lhh.server.common.utils.ImageUtil;
import org.lhh.server.common.utils.JSONArray;
import org.lhh.server.common.utils.JSONObject;
import org.lhh.server.core.codec.database.DatabaseCodec;
import org.lhh.server.core.codec.trans.TransMetaCodec;
import org.lhh.server.service.IDatabaseService;
import org.pentaho.di.core.plugins.DatabasePluginType;
import org.pentaho.di.core.plugins.PluginInterface;
import org.pentaho.di.core.plugins.PluginRegistry;
import org.pentaho.di.trans.TransMeta;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping(value = "/database")
public class DatabaseController {

    private static List<String> types = Arrays.asList("GREENPLUM", "MSSQL", "MYSQL", "ORACLE", "POSTGRESQL");

    @Autowired
    private IDatabaseService databaseService;

    @UserAuth
    @LogCatch(value = "数据连接-支持数据库类型")
    @GetMapping(value = "/accessData")
    public JSONResult accessData() {
        PluginRegistry registry = PluginRegistry.getInstance();
        List<PluginInterface> plugins = registry.getPlugins(DatabasePluginType.class);
        JSONArray jsonArray = new JSONArray();
        for (PluginInterface pluginInterface : plugins) {
            String id = pluginInterface.getIds()[0];
            if (!types.contains(id))
                continue;
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id", id);
            jsonObject.put("name", pluginInterface.getName());
            jsonObject.put("image", ImageUtil.getDBImage(id));
            jsonArray.add(jsonObject);
        }
        return JSONResult.ok(jsonArray);
    }

    @UserAuth
    @LogCatch(value = "数据连接-保存")
    @PutMapping(value = "/save")
    public JSONResult save(@RequestBody DatabaseCodec databaseCodec) {
        databaseService.save(databaseCodec);
        return JSONResult.ok();
    }

    @UserAuth
    @LogCatch(value = "数据连接-测试连接")
    @PutMapping(value = "/test")
    public JSONResult test(@RequestBody DatabaseCodec databaseCodec) {
        databaseService.test(databaseCodec);
        return JSONResult.ok();
    }

    @UserAuth
    @LogCatch(value = "数据连接-复制")
    @PutMapping(value = "/copy")
    public JSONResult copy(@RequestBody DatabaseCodec databaseCodec) {
        databaseService.copy(databaseCodec);
        return JSONResult.ok();
    }

    @UserAuth
    @LogCatch(value = "数据连接-删除")
    @DeleteMapping(value = "/remove/{name}")
    public JSONResult list(@PathVariable String name) {
        databaseService.remove(name);
        return JSONResult.ok();
    }

    @UserAuth
    @LogCatch(value = "数据连接-数据源列表")
    @GetMapping(value = "/list")
    public JSONResult getList() {
        return databaseService.getList();
    }

    @UserAuth
    @LogCatch(value = "数据连接-数据源列表")
    @GetMapping(value = "/getDataBaseById/{id}")
    public JSONResult getDataBaseById(@PathVariable Long id) {
        return JSONResult.ok(databaseService.getDataBaseById(id));
    }

    @UserAuth
    @LogCatch(value = "数据连接-库表目录")
    @PostMapping(value = "/explorer")
    public JSONResult explorer(@RequestParam String connection
            , @RequestParam(defaultValue = "") String nodeId
            , @RequestParam(defaultValue = "") String tableName) {
        return JSONResult.ok(databaseService.explorer(connection, nodeId, tableName));
    }

    @UserAuth
    @LogCatch(value = "数据连接-获取表字段")
    @PostMapping(value = "/tableFields")
    public JSONResult tableFields(@RequestParam String graphXml
            , @RequestParam String connection
            , @RequestParam(defaultValue = "") String schema
            , @RequestParam String table) {
        return JSONResult.ok(databaseService.tableFields(graphXml, connection, schema, table));
    }

    @UserAuth
    @LogCatch(value = "数据连接-获取SQL查询字段")
    @PostMapping(value = "/getSQL")
    public JSONResult getSQL(@RequestParam String graphXml
            , @RequestParam String connection
            , @RequestParam(defaultValue = "") String schema
            , @RequestParam String table
            , @RequestParam(defaultValue = "true") boolean yes) {
        TransMeta transMeta = (TransMeta) TransMetaCodec.decode(graphXml);
        return JSONResult.ok(databaseService.getSQL(transMeta, connection, schema, table, yes));
    }

}
